<?php
session_start();
if (empty($_SESSION['username'])) {
    echo '<script type="text/javascript">window.location.href="../index.php";</script>';
} else {
    $now = time();
    if ($now > $_SESSION['expire']) {
        session_destroy();
        echo '<script type="text/javascript">var r=confirm("Session expire (30 mins)!"); if(r==true){window.location.href="../index.php";}else{window.location.href="index.php";}</script>';
    } else {
        require '../model-db-connection/config.php';
        $config = require '../model-db-connection/qrc_conf.properties.php';
        $searchCondition = $_GET['searchCondition'];
        $project_id = $_GET['project_id'];
        $project_code = $_GET['project_code'];
        $document_no = $_GET['document_no'];
        $po_no = $_GET['po_no'];
        $po_owner = $_GET['po_owner'];
        $po_sender = $_GET['po_sender'];
        $po_issue_date = $_GET['po_issue_date'];
        $po_order_type = $_GET['po_order_type'];
    }
}
?>
<script src="../js/jquery.dataTables.js"></script>
<script src="../js/plugins/datatables/ColReorderWithResize.js"></script>
<script src="../js/plugins/datatables/colvis/dataTables.ColVis.min.js"></script>
<script src="../js/plugins/datatables/tabletools/ZeroClipboard.js"></script>
<script src="../js/plugins/datatables/tabletools/dataTables.TableTools.min.js"></script>
<script src="../js/dataTables.bootstrap.js"></script>

<link rel="stylesheet" href="../css/plugins/dataTables.bootstrap.css">
<link rel="stylesheet" href="../css/ColVis.css">


<script type="text/javascript" src="../js/zebra_dialog.js"></script>
<link rel="stylesheet" href="../css/zebra_dialog.css" type="text/css">

<table class="table table-striped table-bordered table-hover" id="dataTables-example" width="100%">
    <!-- Table heading -->
    <thead>
        <tr>
            <th data-class="expand" class="center">PO Code</th>
            <th data-class="expand" class="center">Project Name</th>
            <th class = "center">Project Code</th>
            <th class = "center">Document No</th>
            <th class = "center">PO No.</th>
            <th class = "center">PO Owner</th>
            <th class = "center">PO Sender</th>
            <!--<th data-hide="phone,tablet">Home Plan</th>-->
            <!--<th data-hide="phone,tablet">Home Plot</th>-->
            <!--<th data-hide="phone,tablet">Issue Date</th>-->
            <!--<th data-hide="phone,tablet">Order type</th>-->
            <!--<th data-hide="phone,tablet">Quantity</th>-->
            <!--<th data-hide="phone,tablet">Plan Size</th>-->
            <!--<th data-hide="phone,tablet">Unit Prices</th>-->
            <th data-hide="phone,tablet">Amount</th>
            <!--<th data-hide="phone,tablet">Grand Total included VAT 7%</th>-->
            <!--<th data-hide="phone,tablet">Supervisor Control</th>-->
            <!--<th data-hide="phone,tablet">Project Manager</th>-->
            <!--<th data-hide="phone,tablet">Project Foreman</th>-->
            <!--<th data-hide="phone,tablet">Remark</th>-->
            <!--<th data-hide="phone,tablet">PO Image</th>-->
            <th></th>
        </tr>
    </thead>
    <!-- // Table heading END -->
    <!-- Table body -->
    <tbody>
        <?php
        if ($searchCondition == "All") {
            $sqlSelectMemberAll = "SELECT qp.PO_PROJECT_NAME as PO_PROJECT_NAME,"
                    . "qp.PO_PROJECT_CODE as PO_PROJECT_CODE,"
                    . "qp.PO_DOCUMENT_NO as PO_DOCUMENT_NO,"
                    . "qp.PO_PO_NO as PO_PO_NO,"
                    . "qp.PO_HOME_PLAN as PO_HOME_PLAN,"
                    . "qp.PO_HOME_PLOT as PO_HOME_PLOT,"
                    . "qp.PO_OWNER as PO_OWNER,"
                    . "qp.PO_SENDER as PO_SENDER,"
                    . "qp.PO_ISSUE_DATE as PO_ISSUE_DATE,"
                    . "qts.service_name as order_type_name,"
                    . "qp.PO_QUANTITY as PO_QUANTITY,"
                    . "qp.PO_PLAN_SIZE as PO_PLAN_SIZE,"
                    . "qp.PO_UNIT_PRICE as PO_UNIT_PRICE,"
                    . "qp.PO_AMOUNT as PO_AMOUNT,"
                    . "qp.PO_VAT as  PO_VAT,"
                    . "qp.PO_SUPERVISOR_ID as PO_SUPERVISOR_ID,"
                    . "qp.PO_PROJECT_MANAGER_ID as PO_PROJECT_MANAGER_ID,"
                    . "qp.PO_PROJECT_FOREMAN_ID as PO_PROJECT_FOREMAN_ID,"
                    . "qp.PO_ID as PO_ID,"
                    . "qp.PO_REMARK as PO_REMARK"
                    . " FROM QRC_PO qp"
                    . " LEFT JOIN QRC_TYPE_OF_SERVICE qts on qp.po_order_type_id = qts.service_id"
                    . " ORDER BY qp.PO_PROJECT_CODE DESC;";
        } else {
            $checkProjectid = !empty($project_id) ? " AND qp.PO_PROJECT_CODE LIKE '%$project_id%'" : "";
            $checkProjecCode = !empty($project_code) ? " AND qp.PO_PROJECT_CODE LIKE '%$project_code%'" : "";
            $checkDocNo = !empty($document_no) ? " AND qp.PO_DOCUMENT_NO LIKE '%$document_no%'" : "";
            $checkPoNo = !empty($po_no) ? " AND qp.PO_PO_NO LIKE '%$po_no%'" : "";
            $checkPoOwner = !empty($po_owner) ? " AND qp.PO_OWNER LIKE '%$po_owner%'" : "";
            $checkPoSender = !empty($po_sender) ? " AND qp.PO_SENDER LIKE '%$po_sender%'" : "";
            $checkPoIssueDate = !empty($po_issue_date) ? " AND qp.PO_ISSUE_DATE BETWEEN '$po_issue_date' AND '$po_issue_date'" : "";
            $checkPoOrderType = !empty($po_order_type) ? " AND qp.PO_ORDER_TYPE_ID LIKE '$po_order_type'" : "";
            $sqlSelectMemberAll = "SELECT qp.PO_PROJECT_NAME as PO_PROJECT_NAME,"
                    . "qp.PO_PROJECT_CODE as PO_PROJECT_CODE,"
                    . "qp.PO_DOCUMENT_NO as PO_DOCUMENT_NO,"
                    . "qp.PO_PO_NO as PO_PO_NO,"
                    . "qp.PO_HOME_PLAN as PO_HOME_PLAN,"
                    . "qp.PO_HOME_PLOT as PO_HOME_PLOT,"
                    . "qp.PO_OWNER as PO_OWNER,"
                    . "qp.PO_SENDER as PO_SENDER,"
                    . "qp.PO_ISSUE_DATE as PO_ISSUE_DATE,"
                    . "qts.service_name as order_type_name,"
                    . "qp.PO_QUANTITY as PO_QUANTITY,"
                    . "qp.PO_PLAN_SIZE as PO_PLAN_SIZE,"
                    . "qp.PO_UNIT_PRICE as PO_UNIT_PRICE,"
                    . "qp.PO_AMOUNT as PO_AMOUNT,"
                    . "qp.PO_VAT as  PO_VAT,"
                    . "qp.PO_SUPERVISOR_ID as PO_SUPERVISOR_ID,"
                    . "qp.PO_PROJECT_MANAGER_ID as PO_PROJECT_MANAGER_ID,"
                    . "qp.PO_PROJECT_FOREMAN_ID as PO_PROJECT_FOREMAN_ID,"
                    . "qp.PO_ID as PO_ID,"
                    . "qp.PO_REMARK as PO_REMARK"
                    . " FROM QRC_PO qp"
                    . " LEFT JOIN qrc_type_of_service qts on qp.po_order_type_id = qts.service_id"
                    . " WHERE 1=1"
                    . $checkProjectid
                    . $checkProjecCode
                    . $checkDocNo
                    . $checkPoNo
                    . $checkPoOwner
                    . $checkPoSender
                    . $checkPoIssueDate
                    . $checkPoOrderType;
        }
//                echo $sqlSelectMemberAll;
        $sqlGetAllData = mysql_query($sqlSelectMemberAll);
        if (mysql_num_rows($sqlGetAllData) >= 1) {
            while ($row = mysql_fetch_assoc($sqlGetAllData)) {
                echo '<tr class = "gradeX">';
                echo '<td>' . $row['PO_ID'] . '</td>';
                echo '<td>' . $row['PO_PROJECT_NAME'] . '</td>';
                echo '<td>' . $row['PO_PROJECT_CODE'] . '</td>';
                echo '<td>' . $row['PO_DOCUMENT_NO'] . '</td>';
                echo '<td>' . $row['PO_PO_NO'] . '</td>';
                echo '<td>' . $row['PO_OWNER'] . '</td>';
                echo '<td>' . $row['PO_SENDER'] . '</td>';
//                echo '<td>' . $row['PO_HOME_PLAN'] . '</td>';
//                echo '<td>' . $row['PO_HOME_PLOT'] . '</td>';
//                echo '<td>' . $row['PO_ISSUE_DATE'] . '</td>';
//                echo '<td>' . $row['order_type_name'] . '</td>';
//                echo '<td>' . $row['PO_QUANTITY'] . '</td>';
//                echo '<td>' . $row['PO_PLAN_SIZE'] . '</td>';
//                echo '<td>' . $row['PO_UNIT_PRICE'] . '</td>';
                echo '<td>' . $row['PO_AMOUNT'] . '</td>';
//                echo '<td>' . $row['PO_VAT'] . '</td>';
//                echo '<td>' . $row['PO_SUPERVISOR_ID'] . '</td>';
//                echo '<td>' . $row['PO_PROJECT_MANAGER_ID'] . '</td>';
//                echo '<td>' . $row['PO_PROJECT_FOREMAN_ID'] . '</td>';
//                echo '<td>' . $row['PO_REMARK'] . '</td>';
//                $sqlSelectImageByID = "SELECT IMAGE_PATH as IMAGE_PATH FROM QRC_PO_IMAGE WHERE TEMP_PO_ID LIKE '" . $row['PO_ID'] . "'";
//                $queryGetFilePath = mysql_query($sqlSelectImageByID);
//                $strBuilding = "";
//                while ($rowq = mysql_fetch_assoc($queryGetFilePath)) {
//                    $strBuilding.='<d class="fancybox-effects-d" href="../images/uploads/' . $rowq['IMAGE_PATH'] . '" title="' . $rowq['IMAGE_PATH'] . '" onlick="changeAttrHref()"><img src="../images/uploads/' . $rowq['IMAGE_PATH'] . '" alt="Smiley face" width="200"></d>';
//                }
//                echo '<td>' . $strBuilding . '</td>';

                echo '<td>';

                echo '<div class = "btn-group margin-bottom-20">';
                echo '<button type = "button" class = "btn btn-default dropdown-toggle btn-xs" data-toggle = "dropdown">Actions <span class = "caret"></span></button>';

                echo '<ul class = "dropdown-menu" role = "menu">';
                echo '<li><a href = "#" class="btn-xs" onclick=loadOrder()><i class = "fa fa-rss"></i> Assign (มอบหมาย)</a></li>';
                echo '<li><a href = "#modal-po" class="btn-xs" data-toggle = "modal" onclick=editPO("' . $row['PO_ID'] . '")><i class = "fa fa-edit"></i> Edit (แก้ไข)</a></li>';
                echo '<li><a href = "#" onclick=viewClick("' . $row['PO_ID'] . '") class="btn-xs")><i class = "fa fa-eye"></i> View (ดูข้อมูล)</a></li>';
                echo '<li class = "divider"></li>';
                echo '<li><a href = "#" onclick=deletePO("' . $row['PO_ID'] . '","' . $row['PO_PO_NO'] . '")><i class = "fa fa-trash-o"></i> Delete (ลบ)</a></li>';
                echo '</ul>';
                echo '</div>';
                echo '</td>';
                echo '</tr>';
                $strBuilding = "";
            }
        }
        ?>
    </tbody>
    <!-- // Table body END -->

</table>


<script>
    $(document).ready(function() {

        $('#dataTables-example').dataTable({
            "order": [[0, "desc"]]
        });
    });
    function viewClick(po_id) {
        $.Zebra_Dialog({
            source: {'ajax': 'AjaxViewContent.php?po_id=' + po_id},
            width: 900,
            max_height: 500,
            title: 'PO Detail'
        });
    }
</script>